package com.cskaoyan.controller;


import com.cskaoyan.bean.bo.WxOrderCommentBo;
import com.cskaoyan.bean.bo.WxOrderRefundBO;
import com.cskaoyan.bean.bo.WxOrderSubmitBO;
import com.cskaoyan.bean.common.BaseRespVo;
import com.cskaoyan.bean.po.MarketUser;
import com.cskaoyan.bean.vo.WXOrderListVo;
import com.cskaoyan.bean.vo.WxOrderGoodsVO;
import com.cskaoyan.bean.vo.WxOrderSubmitVO;
import com.cskaoyan.service.WxOrderService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author zhuqiushi
 * @since 2022/09/09 21:22
 */
@RestController
@RequestMapping("wx/order")
public class WxOrderController {
    @Autowired
    WxOrderService wxOrderService;

    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId) {
        BaseRespVo baseRespVo = wxOrderService.selectMessageById(orderId);
        return baseRespVo;
    }

    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody WxOrderCommentBo commentBo) {
        Session session = SecurityUtils.getSubject().getSession();
        MarketUser info = (MarketUser) session.getAttribute("info");
        Integer id = info.getId();
        BaseRespVo baseRespVo = wxOrderService.insertMessage(commentBo,id);
        return baseRespVo;
    }



    /**
     * @Description: 获取订单表/wx/order/list
     * @Description: http://localhost:8083/wx/order/list?showType=0&page=1&limit=10
     * @date: 2022/9/11 15:20
     * @author: zyc
     * @since JDK 1.8
     */
    @GetMapping("list")
    public BaseRespVo getOrderList(Integer showType, Integer page, Integer limit) {
        Session session = SecurityUtils.getSubject().getSession();
        MarketUser info = (MarketUser) session.getAttribute("info");
        WXOrderListVo listVo = wxOrderService.getWXOrderList(showType, page, limit,info.getId());
        return BaseRespVo.ok(listVo);
    }

    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map confirmMap) {
        Integer orderId = (Integer) confirmMap.get("orderId");
        int code = wxOrderService.confirm(orderId);
        return BaseRespVo.ok();
    }

    /*
     * /wx/order/goods?orderId=274&goodsId=1181057
     * GET
     * */
    @RequestMapping("goods")
    public BaseRespVo<WxOrderGoodsVO> goods(Integer orderId, Integer goodsId) {
        //调用service查询
        WxOrderGoodsVO wxOrderGoodsVO = wxOrderService.goods(orderId, goodsId);
        return BaseRespVo.ok(wxOrderGoodsVO);
    }

    //退货
    @RequestMapping("refund")
    public BaseRespVo orderRefund(@RequestBody WxOrderRefundBO wxOrderRefundBO) {
        //调用service查询
        Integer orderId = wxOrderRefundBO.getOrderId();
        wxOrderService.orderRefund(orderId);
        return BaseRespVo.ok();
    }

    @RequestMapping("delete")
    public BaseRespVo orderDelete(@RequestBody WxOrderRefundBO wxOrderRefundBO) {
        //调用service查询
        Integer orderId = wxOrderRefundBO.getOrderId();
        wxOrderService.orderDelete(orderId);
        return BaseRespVo.ok();
    }

    /*
     * /wx/order/submit
     * POST
     * */
    @RequestMapping("submit")
    public BaseRespVo<WxOrderSubmitVO> submit(@RequestBody WxOrderSubmitBO wxOrderSubmitBO) {
        Session session = SecurityUtils.getSubject().getSession();
        MarketUser user = (MarketUser) session.getAttribute("info");
        //调用service层操作
        WxOrderSubmitVO wxOrderSubmitVO = wxOrderService.submit(wxOrderSubmitBO, user);
        return BaseRespVo.ok(wxOrderSubmitVO);
    }
}
